package cn.com.easy.phantomjs;

import cn.com.easy.phantomjs.uiils.jsoup.JsoupParserUtils;
import cn.com.easy.phantomjs.uiils.phantomjs.ClassPathUtils;
import cn.com.easy.phantomjs.uiils.phantomjs.PhantomjsUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author Hbys004
 * @create 2019-09-27 17:49\
 * @desc
 **/

public class TestMain {
    final static String tempPath = "D:/temp/img";// 图片保存目录
    final static String BLANK = " ";
    final static String phantomjsPath = ClassPathUtils.getClassesLoadRootPath();

    final static String rasterizejs = phantomjsPath + "phantomjs/examples/rasterize.js";
    final static String binPath = phantomjsPath + "phantomjs/phantomjs-2.1.1-windows/phantomjs.exe";

    public static void main(String[] args) throws Exception {
        String url = "https://www.baidu.com/";//以百度网站首页为例
        printUrlScreen2jpg(url);
    }



    // 执行cmd命令
    public static String cmd(String imgagePath, String url) {
        return binPath + BLANK + rasterizejs + BLANK + url + BLANK + imgagePath;
    }
    //关闭命令
    public static void close(Process process, BufferedReader bufferedReader) throws IOException {
        if (bufferedReader != null) {
            bufferedReader.close();
        }
        if (process != null) {
            process.destroy();
            process = null;
        }
    }
    /**
     * @param url > https://www.cnblogs.com/han108/p/9216583.html
     * @throws IOException
     */
    public static void printUrlScreen2jpg(String url) throws IOException{
        String imgagePath = tempPath+"/"+System.currentTimeMillis()+".png";//图片路径
        //Java中使用Runtime和Process类运行外部程序
        Process process = Runtime.getRuntime().exec(cmd(imgagePath,url));
        InputStream inputStream = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String tmp = "";
        while ((tmp = reader.readLine()) != null) {
            close(process,reader);
        }
        System.out.println("success");
    }


    /**
     * 解析页面功能
     * @throws Exception
     */
    public void parsePage() throws Exception {
        final String PATTERN_UNION = "%s|%s|%s|%s";
        final String TITLE_PATTERN = "//*[@id=\"aMain\"]/div[1]/h2";
        final String AUTHOR_PATTERN = "//*[@id=\"aMain\"]/div[2]";
        final String TIME_PATTERN = "//*[@id=\"aMain\"]/div[3]";
        final String CONTENT_PATTERN = "//*[@id=\"aMain\"]/div[4]";

        String html = PhantomjsUtils.getJsHtmlCotnent("https://p.vivame.net.cn/special/special.html?sid=104000&accessId=167072&appId=life&vid=16900291");
        Document document = Jsoup.parse(html);
        Elements elements = JsoupParserUtils.getJsoupElements(document, "//*[@id=\"feedList\"]/ul/li/a");
        if (null != elements && elements.size() > 0) {
            for (Element element : elements) {
                String subUrl = element.attr("articleurl");
                if (subUrl.contains("//") && !subUrl.contains("://")) {
                    subUrl = subUrl.replace("//", "Https://");
                }
                Document doc = JsoupParserUtils.getDocument(subUrl);
                Elements jsoupElements = JsoupParserUtils.getJsoupElements(doc, String.format(PATTERN_UNION, TITLE_PATTERN, AUTHOR_PATTERN, TIME_PATTERN, CONTENT_PATTERN));

                System.err.println(jsoupElements);
            }
        }
        System.out.println();
    }
}
